package cn.iocoder.yudao.module.acm.controller.admin.cleaningplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanSaveReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanUpdateStatusReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.cleaningplan.CleaningPlanDO;
import cn.iocoder.yudao.module.acm.service.cleaningplan.CleaningPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 机供品餐具清洗计划")
@RestController
@RequestMapping("/acm/cleaning-plan")
@Validated
public class CleaningPlanController {

    @Resource
    private CleaningPlanService cleaningPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建机供品餐具清洗计划")
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:create')")
    public CommonResult<Long> createCleaningPlan(@Valid @RequestBody CleaningPlanSaveReqVO createReqVO) {
        return success(cleaningPlanService.createCleaningPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机供品餐具清洗计划")
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:update')")
    public CommonResult<Boolean> updateCleaningPlan(@Valid @RequestBody CleaningPlanSaveReqVO updateReqVO) {
        cleaningPlanService.updateCleaningPlan(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新机供品餐具清洗计划清洗状态")
    public CommonResult<Boolean> updateCleaningPlanStatus(@Valid @RequestBody CleaningPlanUpdateStatusReqVO updateReqVO) {
        cleaningPlanService.updateCleaningPlanStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机供品餐具清洗计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:delete')")
    public CommonResult<Boolean> deleteCleaningPlan(@RequestParam("id") Long id) {
        cleaningPlanService.deleteCleaningPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机供品餐具清洗计划")
                @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:delete')")
    public CommonResult<Boolean> deleteCleaningPlanList(@RequestParam("ids") List<Long> ids) {
        cleaningPlanService.deleteCleaningPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机供品餐具清洗计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:query')")
    public CommonResult<CleaningPlanRespVO> getCleaningPlan(@RequestParam("id") Long id) {
        CleaningPlanDO cleaningPlan = cleaningPlanService.getCleaningPlan(id);
        return success(BeanUtils.toBean(cleaningPlan, CleaningPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机供品餐具清洗计划分页")
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:query')")
    public CommonResult<PageResult<CleaningPlanRespVO>> getCleaningPlanPage(@Valid CleaningPlanPageReqVO pageReqVO) {
        PageResult<CleaningPlanRespVO> pageResult = cleaningPlanService.getCleaningPlanPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机供品餐具清洗计划 Excel")
    @PreAuthorize("@ss.hasPermission('acm:cleaning-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCleaningPlanExcel(@Valid CleaningPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CleaningPlanRespVO> list = cleaningPlanService.getCleaningPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机供品餐具清洗计划.xls", "数据", CleaningPlanRespVO.class,
                        BeanUtils.toBean(list, CleaningPlanRespVO.class));
    }


}